16 define('DF_LASTPREF', 4);
25 * @todo preferences, OutputPage
29 var $mSource, $mTarget;
30 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
32 var $regexes, $pDays, $pMonths, $pYears;
35 function DateFormatter()
37 global $wgMonthNamesEn, $wgInputEncoding;
39 $this->monthNames
= $this->getMonthRegex();
40 for ( $i=1; $i<=12; $i++
) {
41 $this->xMonths
[strtolower( $wgMonthNamesEn[$i-1] )] = $i;
44 # Attempt at UTF-8 support, untested at the moment
45 if ( $wgInputEncoding == 'UTF-8' ) {
46 $this->regexTrail
= '(?![a-z])/iu';
48 $this->regexTrail
= '(?![a-z])/i';
51 # Partial regular expressions
52 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
53 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
54 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
55 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
56 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
58 # Real regular expressions
59 $this->regexes
[DF_DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
60 $this->regexes
[DF_YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
61 $this->regexes
[DF_MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
62 $this->regexes
[DF_YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
63 $this->regexes
[DF_DM
] = "/{$this->prxDM}{$this->regexTrail}";
64 $this->regexes
[DF_MD
] = "/{$this->prxMD}{$this->regexTrail}";
65 $this->regexes
[DF_ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
66 $this->regexes
[DF_ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
69 # See the comments in replace() for the meaning of the letters
70 $this->keys
[DF_DMY
] = 'jFY';
71 $this->keys
[DF_YDM
] = 'Y jF';
72 $this->keys
[DF_MDY
] = 'FjY';
73 $this->keys
[DF_YMD
] = 'Y Fj';
74 $this->keys
[DF_DM
] = 'jF';
75 $this->keys
[DF_MD
] = 'Fj';
76 $this->keys
[DF_ISO1
] = 'ymd'; # y means ISO year
77 $this->keys
[DF_ISO2
] = 'ymd';
80 $this->targets
[DF_DMY
] = '[[F j|j F]] [[Y]]';
81 $this->targets
[DF_YDM
] = '[[Y]], [[F j|j F]]';
82 $this->targets
[DF_MDY
] = '[[F j]], [[Y]]';
83 $this->targets
[DF_YMD
] = '[[Y]] [[F j]]';
84 $this->targets
[DF_DM
] = '[[F j|j F]]';
85 $this->targets
[DF_MD
] = '[[F j]]';
86 $this->targets
[DF_ISO1
] = '[[Y|y]]-[[F j|m-d]]';
87 $this->targets
[DF_ISO2
] = '[[y-m-d]]';
91 $this->rules
[DF_DMY
][DF_MD
] = DF_DM
;
92 $this->rules
[DF_ALL
][DF_MD
] = DF_MD
;
93 $this->rules
[DF_MDY
][DF_DM
] = DF_MD
;
94 $this->rules
[DF_ALL
][DF_DM
] = DF_DM
;
95 $this->rules
[DF_NONE
][DF_ISO2
] = DF_ISO1
;
98 function reformat( $preference, $text )
100 for ( $i=1; $i<=DF_LAST
; $i++
) {
102 if ( @$this->rules
[$preference][$i] ) {
104 $this->mTarget
= $this->rules
[$preference][$i];
105 } elseif ( @$this->rules
[DF_ALL
][$i] ) {
107 $this->mTarget
= $this->rules
[DF_ALL
][$i];
108 } elseif ( $preference ) {
110 $this->mTarget
= $preference;
115 $text = preg_replace_callback( $this->regexes
[$i], 'wfMainDateReplace', $text );
120 function replace( $matches )
122 global $wgMonthNamesEn;
123 # Extract information from $matches
125 $key = $this->keys
[$this->mSource
];
126 for ( $p=0; $p < strlen($key); $p++
) {
127 if ( $key{$p} != ' ' ) {
128 $bits[$key{$p}] = $matches[$p+
1];
132 $format = $this->targets
[$this->mTarget
];
138 for ( $p=0; $p < strlen( $format ); $p++
) {
141 case 'd': # ISO day of month
142 if ( is_null($bits['d']) ) {
143 $text .= sprintf( '%02d', $bits['j'] );
148 case 'm': # ISO month
149 if ( is_null($bits['m']) ) {
150 $m = $this->makeIsoMonth( $bits['F'] );
151 if ( !$m ||
$m == '00' ) {
161 if ( is_null( $bits['y'] ) ) {
162 $text .= $this->makeIsoYear( $bits['Y'] );
167 case 'j': # ordinary day of month
168 if ( is_null($bits['j']) ) {
169 $text .= IntVal( $bits['d'] );
174 case 'F': # long month
175 if ( is_null( $bits['F'] ) ) {
176 $m = IntVal($bits['m']);
177 if ( $m > 12 ||
$m < 1 ) {
180 $text .= $wgMonthNamesEn[$m-1];
183 $text .= ucfirst( $bits['F'] );
186 case 'Y': # ordinary (optional BC) year
187 if ( is_null( $bits['Y'] ) ) {
188 $text .= $this->makeNormalYear( $bits['y'] );
203 function getMonthRegex()
205 global $wgMonthNamesEn;
206 return implode( '|', $wgMonthNamesEn );
209 # Makes an ISO month, e.g. 02, from a month name
210 function makeIsoMonth( $monthName )
212 $n = $this->xMonths
[strtolower( $monthName )];
213 return sprintf( '%02d', $n );
216 function makeIsoYear( $year )
218 # Assumes the year is in a nice format, as enforced by the regex
219 if ( substr( $year, -2 ) == 'BC' ) {
220 $num = IntVal(substr( $year, 0, -3 )) - 1;
221 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
222 $text = sprintf( '-%04d', $num );
225 $text = sprintf( '%04d', $year );
230 function makeNormalYear( $iso )
232 if ( $iso{0} == '-' ) {
233 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
235 $text = IntVal( $iso );
241 function wfMainDateReplace( $matches )
243 global $wgDateFormatter;
244 return $wgDateFormatter->replace( $matches );